<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-select v-model="dataForm.selectshopid" placeholder= "校区"  style="width:180px;height:50px">
          <el-option v-for="(item, index) in shopids"
                     :label="item.shopName"
                     :key="item.shopName"
                     :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="parentName" style="width:120px;height:50px">
        <el-popover
          ref="treeListPopover"
          placement="bottom-start"
          trigger="click" >
          <el-tree
            :data="treeList"
            :props="treeListTreeProps"
            node-key="id"
            ref="treeListTree"
            @current-change="treeListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:treeListPopover  :readonly="true" placeholder="点击选择分类" class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item style="width:120px;height:50px">
        <el-input v-model="dataForm.studentName" placeholder="姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="dataForm.selDate"
          type="daterange"
          value-format="yyyy-MM-dd"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="创建日期起"
          end-placeholder="创建日期止"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="clearQuery()">重置</el-button>
        <el-button v-if="isAuth('business:cwOrder:save')" type="primary" @click="addOrUpdateHandle()">财务报账</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <!-- <el-table-column type="index" width="50" label="序号">
      </el-table-column> -->
      <el-table-column prop="id" header-align="center" align="center" label="编号">
      </el-table-column>
      <el-table-column prop="shopName" header-align="center" align="center" label="校区">
      </el-table-column>
      <el-table-column prop="categoryName" header-align="center" align="center" label="分类名称">
      </el-table-column>
      <el-table-column prop="studentName" header-align="center" align="center" label="学员姓名">
      </el-table-column>
      <!-- <el-table-column prop="orderStatus" :formatter="statusFormatter" header-align="center" align="center" label="订单状态">
      </el-table-column> -->
      <el-table-column prop="orderAmount" header-align="center" align="center" label="订单金额">
      </el-table-column>
      <!-- <el-table-column prop="serviceCharge" header-align="center" align="center" label="手续费">
      </el-table-column>
      <el-table-column prop="preferentialAmount" header-align="center" align="center" label="优惠金额">
      </el-table-column>
      <el-table-column prop="amountPayable" header-align="center" align="center" label="应付金额">
      </el-table-column>
      <el-table-column prop="amountPaid" header-align="center" align="center" label="已付金额">
      </el-table-column>
      -->
      <el-table-column prop="createAt" header-align="center" align="center" label="创建时间">
      </el-table-column>
      <!-- <el-table-column prop="paymentTime" header-align="center" align="center" label="支付完成时间">
      </el-table-column>
      <el-table-column prop="subscriptionTime" header-align="center" align="center" label="认款时间">
      </el-table-column> -->
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <information v-if="informationVisible" ref="information" @refreshDataList="getDataList"></information>
  </div>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  import AddOrUpdate from './cworder-add-or-update'
  import Information from './cworder-information'
  export default {
    data () {
      return {
        dataForm: {
          id: '',
          orderStatus: '',
          selDate: [],
          selDateOne: [],
          selDateTwo: [],
          selectshopid: '',
          studentName: '',
          parentId: '',
          parentName: ''
        },
        treeList: [],
        treeListTreeProps: {
          label: 'category',
          children: 'children'
        },
        shopids: '',
        options: [{
          value: '0',
          label: '未支付'
        }, {
          value: '1',
          label: '支付中'
        }, {
          value: '2',
          label: '待认款'
        }, {
          value: '3',
          label: '支付完成'
        }, {
          value: '4',
          label: '退费中'
        }, {
          value: '5',
          label: '已退费'
        }, {
          value: '6',
          label: '已取消'
        }],
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick: function (picker) {
              const end = new Date()
              const start = new Date()
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 1)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三天',
            onClick: function (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 2)
              end.setTime(end.getTime() + 3600 * 1000 * 24 * 1)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近七天',
            onClick: function (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 6)
              end.setTime(end.getTime() + 3600 * 1000 * 24 * 1)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        informationVisible: false
      }
    },
    components: {
      AddOrUpdate,
      Information
    },
    activated () {
      this.getDataList()
      this.getShopInfo()
      this.getFatherCategory()
    },
    methods: {
      clearQuery () {
        this.dataForm.selDate = []
        this.dataForm.selDateOne = []
        this.dataForm.selDateTwo = []
        this.dataForm.selectshopid = ''
        this.dataForm.studentName = ''
        this.dataForm.parentId = ''
        this.dataForm.parentName = ''
      },
      // 查询一级分类
      getFatherCategory () {
        this.$http({
          url: this.$http.adornUrl('/business/cwCategory/list'),
          method: 'get',
          params: this.$http.adornParams({
          })
        }).then(({
          data
        }) => {
          this.treeList = treeDataTranslate(data, 'id', 'pid')
        })
      },
      // 菜单树选中
      treeListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parentId = data.id
        this.dataForm.parentName = data.category
        this.pClose()
      },
      pClose () {
        this.$refs[`treeListPopover`].doClose()
      },
      // 获取分校信息-列表
      getShopInfo () {
        this.$http({
          url: this.$http.adornUrl('/business/student/getShopInfo'),
          mothod: 'get',
          params: {'shopId': ''}
        }).then(({data}) => {
          this.shopids = data.list
        })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/business/cwOrder/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'id': this.dataForm.id,
            'createStartDate': this.dataForm.selDate ? this.dataForm.selDate[0] : '',
            'createEntDate': this.dataForm.selDate ? this.dataForm.selDate[1] : '',
            'shopId': this.dataForm.selectshopid,
            'studentName': this.dataForm.studentName,
            'parentId': this.dataForm.parentId
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
          this.$refs.addOrUpdate.getFatherCategory()
          this.$refs.addOrUpdate.queryListByShop()
        })
      },
      // 详情
      informationHandle (id) {
        this.informationVisible = true
        this.$nextTick(() => {
          this.$refs.information.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/business/cwOrder/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 刷新
      reload () {
        this.dataForm.id = ''
        this.dataForm.orderStatus = ''
        this.dataForm.selDate = []
        this.dataForm.selDateOne = []
        this.dataForm.selDateTwo = []
        this.getDataList()
      },
      // 格式化订单状态
      statusFormatter (row, column) {
        let orderStatus = row.orderStatus
        if (orderStatus === 0) {
          return '未支付'
        } else if (orderStatus === 1) {
          return '支付中'
        } else if (orderStatus === 2) {
          return '待认款'
        } else if (orderStatus === 3) {
          return '支付完成'
        } else if (orderStatus === 4) {
          return '退费中'
        } else if (orderStatus === 5) {
          return '已退费'
        } else if (orderStatus === 6) {
          return '已取消'
        } else {
          return ''
        }
      }
    }
  }
</script>
